package com.zyp.demo.web.admin;

import com.zyp.demo.po.Blog;
import com.zyp.demo.po.BlogQuery;
import com.zyp.demo.po.User;
import com.zyp.demo.service.BlogService;
import com.zyp.demo.service.TagsService;
import com.zyp.demo.service.TypeService;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@RequestMapping("/admin")
@Controller
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TagsService tagsService;
    @Autowired
    private TypeService typeService;
   @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 5,sort = {"updateTime"},
           direction = Sort.Direction.DESC) Pageable pageable,Model model,BlogQuery blog){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs";

    }
    @PostMapping("/blogs/search")
    public String blogs_search(@PageableDefault(size = 5,sort = {"updateTime"},
            direction = Sort.Direction.DESC) Pageable pageable, Model model, BlogQuery blog){
       model.addAttribute("page",blogService.listBlog(pageable,blog));
       return "admin/blogs :: blogList";//返回的admin/blogs下 表单的bloglist数据
    }
    @GetMapping("/blogs/input")
    public String blogs_input(Model model){
        model.addAttribute("blog",new Blog());//初始化博客。。。不会报错
        setTypeAndTag(model);
       return "admin/blogs-input";
    }
    @PostMapping("/blogs")
    public String post(Blog blog, HttpSession session, RedirectAttributes attributes) throws NotFoundException {
       blog.setUser((User) session.getAttribute("user"));/**/
       blog.setType(typeService.getType(blog.getType().getId()));/*获取前端选择的分类*/
       blog.setTags(tagsService.listTags(blog.getTagIds()));/*获取前端选择的标签id*/
        Blog b;
       if (blog.getId()==null){
          b=blogService.saveBlog(blog);
       }else {
           b=blogService.updateBlog(blog.getId(),blog);
       }
       if (b==null){
           attributes.addFlashAttribute("message","操作失败");
       }else {
           attributes.addFlashAttribute("message","操作成功");
       }
       return "redirect:/admin/blogs";
    }

    private void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.listType());//初始化分类 （渲染页面）
        model.addAttribute("tags",tagsService.listTags());//初始化标签 (渲染页面)
    }
    /*修改博客*/
    @GetMapping("/blogs/{id}/input")
    public String edit_blog(@PathVariable Long id,Model model){
        setTypeAndTag(model);
      /*  model.addAttribute("blog",model.addAttribute(blogService.getBlog(id))); error*/
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }
    /*删除博客*/
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/blogs";
    }

    @GetMapping("/system")
    public String systemInfo(){
        return "admin/system";
    }
}
